// 无参无返回值的函数
func sayHello(){
    print("Hello, World!")
}
sayHello()

// 有参数的函数
func greet(name:String){
    print("hello,\(name)")
}
greet(name: "Alice")

// 有返回值的函数
func add(a:Int,b:Int)->Int{
    return a+b  
}
let sum = add(a:3,b:5)
print("Sum is \(sum)")

// 参数标签和参数名
func multiply(_ a:Int, by b:Int)->Int{
    return a*b
}
let product = multiply(3, by: 5)
print("Product is \(product)")


// 默认参数值
func greetUser(name:String="Guest"){
    print("Hello, \(name)")
}
greetUser() // 使用默认参数值
greetUser(name: "Alice") // 使用自定义参数值

// 可变参数
func sumOfNumbers(_ numbers:Int...)->Int{
    var sum = 0
    for number in numbers{
        sum += number
    }
    return sum
}
let total = sumOfNumbers(1, 2, 3, 4, 5)
print("Total is \(total)") // 输出：Total is 15


// inout参数
func swapValues(_ a:inout Int, _ b:inout Int){
    let temp = a
    a = b
    b = temp
}
var x = 10
var y = 20
swapValues(&x, &y)
print("x is \(x), y is \(y)") // 输出：x is 20, y is 10


// 函数类型
func add1(a: Int, b: Int) -> Int {
    return a + b
}
let operation:(Int, Int) -> Int = add1
let result1 = operation(3, 5)
print("Result is \(result1)") // 输出：Result is 8

// 函数作为参数
func performOperation(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) -> Int {
    return operation(a, b)
}
let result2 = performOperation(3, 5, operation: add1)
print("Result is \(result2)") // 输出：Result is 8

// 函数作为返回值
func makeIncrementer(incrementAmount: Int) -> (Int) -> Int {
    func increment(number: Int) -> Int {
        return number + incrementAmount
    }
    return increment
}
let incrementByTwo = makeIncrementer(incrementAmount: 2)
let result3 = incrementByTwo(3)
print("Result is \(result3)") // 输出：Result is 5

// 嵌套函数
func outerFunction() {
    func innerFunction() {
        print("This is an inner function.")
    }
    innerFunction()
}
outerFunction() // 输出：This is an inner function.

// 函数重载
func add3(a: Int, b: Int) -> Int {
    return a + b
}
func add3(a: Double, b: Double) -> Double {
    return a + b
}
let intResult = add(a: 3, b: 5) // 调用整型版本